﻿using MvcApplication.Entity;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using MvcApplication.Interface;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using MvcApplication.Business;

namespace MvcApplication.Controllers
{
    public class ImageController : Controller
    {
        IImageService _imageService;

        public ImageController(IImageService imageService)
        {
            _imageService = imageService;
        }

        /// <summary>
        /// 根据图片地址和指定大小显示图片
        /// </summary>
        /// <param name="path"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public ActionResult Index(string path, int? w, int? h)
        {
            Response.AppendHeader("Content-Disposition", "inline;filename=z.jpg");
            if (w == null && h == null) return File(PictureBiz.GetBytes(path), "image/jpeg");

            var model = ThumbnailMode.固定高宽裁减;
            if (h == null) model = ThumbnailMode.固定宽度;
            if (w == null) model = ThumbnailMode.固定高度;

            int width = w ?? 800;
            int height = h ?? 400;

            var image = _imageService.Thumbnail(PictureBiz.GetDiskPath(path), width, height, model);
            if (image == null) return null;
            return File(image, "image/jpeg");

        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost, ActionName("index")]
        public ActionResult Upload()
        {
            var pictures = _imageService.Upload().ToList();
            pictures = _imageService.Save(pictures);

            var paths = pictures.Select(p => p.Path);
            var result = paths.Select(p => string.Format("/image?path={0}", p));

            return Json(new ImageUploadResult
            {
                Success = true,
                Path = paths.FirstOrDefault(),
                PictureURL = result.FirstOrDefault()
            });
        }

        public ActionResult Edit(ImageEditParam param)
        {
            return View(param);
        }

        public ActionResult Preview(string path,
            int displayWidth,
            int displayHeight,
            int leftTopX,
            int leftTopY,
            int width,
            int height,
            bool save = false)
        {
            byte[] image = _imageService.Preview(path, displayWidth, displayHeight, leftTopX, leftTopY, width, height);

            if (image == null) return null;
            if (save)
            {
                var savePath = PictureBiz.GenerateSavePath();
                System.IO.File.WriteAllBytes(savePath, image);
                return Json(savePath);
            }
            else
            {
                Response.AppendHeader("Content-Disposition", "inline;filename=preview.jpg");
                return File(image, "image/jpeg");
            }
        }

        [HttpPost]
        public JsonResult Download(string url)
        {
            var result = _imageService.Download(url);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
